## Sid Meier's Civilization 4
## Copyright Firaxis Games 2005
##
## Implementaion of miscellaneous game functions

import CvUtil
from CvPythonExtensions import *

# Added by Gerikes for Civcraft: SC
import CivcraftSCResources
# End Added by Gerikes for Civcraft: SC


# globals
gc = CyGlobalContext()

class CvGameUtils:
	"Miscellaneous game functions"
	def __init__(self): 
		pass
	
	def isVictoryTest(self):
		if ( gc.getGame().getElapsedGameTurns() > 10 ):
			return True
		else:
			return False

	def isPlayerResearch(self, argsList):
		ePlayer = argsList[0]
		return True

	def createBarbarianCities(self):
		return False
		
	def createBarbarianUnits(self):
		return False
		
	def skipResearchPopup(self,argsList):
		ePlayer = argsList[0]
		return False
		
	def showTechChooserButton(self,argsList):
		ePlayer = argsList[0]
		return True

	def getFirstRecommendedTech(self,argsList):
		ePlayer = argsList[0]
		return TechTypes.NO_TECH

	def getSecondRecommendedTech(self,argsList):
		ePlayer = argsList[0]
		eFirstTech = argsList[1]
		return TechTypes.NO_TECH

	def skipProductionPopup(self,argsList):
		pCity = argsList[0]
		return False
		
	def showExamineCityButton(self,argsList):
		pCity = argsList[0]
		return True

	def getRecommendedUnit(self,argsList):
		pCity = argsList[0]
		return UnitTypes.NO_UNIT

	def getRecommendedBuilding(self,argsList):
		pCity = argsList[0]
		return BuildingTypes.NO_BUILDING

	def updateColoredPlots(self):
		return False

	def isActionRecommended(self,argsList):
		pUnit = argsList[0]
		iAction = argsList[1]
		return False

	def cannotHandleAction(self,argsList):
		pPlot = argsList[0]
		iAction = argsList[1]
		bTestVisible = argsList[2]
		return False

	def cannotSelectionListMove(self,argsList):
		pPlot = argsList[0]
		bAlt = argsList[1]
		bShift = argsList[2]
		bCtrl = argsList[3]
		return False

	def cannotSelectionListGameNetMessage(self,argsList):
		eMessage = argsList[0]
		iData2 = argsList[1]
		iData3 = argsList[2]
		iData4 = argsList[3]
		iFlags = argsList[4]
		bAlt = argsList[5]
		bShift = argsList[6]
		return False

	def cannotDoControl(self,argsList):
		eControl = argsList[0]
		return False

	def canResearch(self,argsList):
		ePlayer = argsList[0]
		eTech = argsList[1]
		bTrade = argsList[2]
		return False

	def cannotResearch(self,argsList):
		ePlayer = argsList[0]
		eTech = argsList[1]
		bTrade = argsList[2]
		return False

	def canDoCivic(self,argsList):
		ePlayer = argsList[0]
		eCivic = argsList[1]
		return False

	def cannotDoCivic(self,argsList):
		ePlayer = argsList[0]
		eCivic = argsList[1]
		return False
		
	def canTrain(self,argsList):
		pCity = argsList[0]
		eUnit = argsList[1]
		bContinue = argsList[2]
		bTestVisible = argsList[3]
		return False

	def cannotTrain(self,argsList):
		pCity = argsList[0]
		eUnit = argsList[1]
		bContinue = argsList[2]
		bTestVisible = argsList[3]

		# Added by Gerikes for built-at and building prereqs.
		buildingBuiltAt = gc.getUnitInfo(eUnit).getBuildingToBuildAt()
		if (buildingBuiltAt == BuildingTypes.NO_BUILDING):
                    return True
                if (not pCity.hasBuilding(buildingBuiltAt)):
                    return True

                if (bTestVisible):
                    return False
                
                for i in range(0, gc.getNumBuildingInfos()):
                    if gc.getUnitInfo(eUnit).getBuildingPrereqs(i):
                        if (not (gc.getPlayer(pCity.getOwner()).hasBuilding(i)) ):
                            return True

                    if gc.getUnitInfo(eUnit).getBuildingInCityPrereqs(i):
                        if (not (pCity.hasBuilding(i) ) ):
                            return True

		# End Added by Gerikes for built-at and building prereqs.
		return False

	def canConstruct(self,argsList):
		pCity = argsList[0]
		eBuilding = argsList[1]
		bContinue = argsList[2]
		bTestVisible = argsList[3]
		bIgnoreCost = argsList[4]
		return False

	def cannotConstruct(self,argsList):
		pCity = argsList[0]
		eBuilding = argsList[1]
		bContinue = argsList[2]
		bTestVisible = argsList[3]
		bIgnoreCost = argsList[4]

                # Added by Gerikes for only allowing buildings with correct improvement
                if (pCity.getNumBuildings() == 0):
                    if (pCity.plot().getImprovementType() == ImprovementTypes.NO_IMPROVEMENT):
                        # If there's no improvement, then you can't build. Strange, we should always have an improvement.
                        CvUtil.pyPrint("No improvement found on plot %d, %d when trying to build." % (pCity.getX(), pCity.getY()) )
                        return True


                    # Added by Gerikes for only allowing buildings with correct improvement
                    buildingName = gc.getBuildingInfo(eBuilding).getType()
                    improvementName = gc.getImprovementInfo(pCity.plot().getImprovementType()).getType()
                    CvUtil.pyPrint("Checking to build %s on plot with improvement %s" % (buildingName, improvementName))
                    

                    # If the improvement is not the name of the building, then we can't build.
                    if (improvementName != "IMPROVEMENT_%s" % buildingName):
                        return True
                        
                # End Added by Gerikes for building capital buildings

		return False

	def canCreate(self,argsList):
		pCity = argsList[0]
		eProject = argsList[1]
		bContinue = argsList[2]
		bTestVisible = argsList[3]
		return False

	def cannotCreate(self,argsList):
		pCity = argsList[0]
		eProject = argsList[1]
		bContinue = argsList[2]
		bTestVisible = argsList[3]
		return False

	def canMaintain(self,argsList):
		pCity = argsList[0]
		eProcess = argsList[1]
		bContinue = argsList[2]
		return False

	def cannotMaintain(self,argsList):
		pCity = argsList[0]
		eProcess = argsList[1]
		bContinue = argsList[2]
		return False

	def AI_chooseTech(self,argsList):
		ePlayer = argsList[0]
		bFree = argsList[1]
		return TechTypes.NO_TECH

	def AI_chooseProduction(self,argsList):
		pCity = argsList[0]
		return False

	def AI_unitUpdate(self,argsList):
		pUnit = argsList[0]

		# Added by Gerikes for harvester
                pPlot = pUnit.plot()
                pGroup = pUnit.getGroup()
                pPlayer = gc.getPlayer(pUnit.getOwner())
                
                ####################
                # AUTOMATE_HARVEST
                ####################
                if (pGroup.getAutomateType() == AutomateTypes.AUTOMATE_HARVEST):
                    # Increment the harvest count for every unit in the group
                    for pLoopUnit in pGroup.getUnits():
                        bonusOnPlot = pPlot.getBonusType(-1)

                        # Increment the number of turns, in case it's a multi-turn harvest.
                        pLoopUnit.changeHarvestTurns(1)

                        # If we're harvested the number of turns needed, do a harvest routine
                        if (gc.getBonusInfo(bonusOnPlot).getHarvestTurns() == pLoopUnit.getHarvestTurns()):
                            # Should probably have a better way of doing this. Oh well.
                            if (gc.getBonusInfo(bonusOnPlot).getType() == "BONUS_MINERAL_DEPOSIT"):
                                pPlayer.changeGold(CivcraftSCResources.getResourcesForHarvest(pPlot, pUnit.getOwner()))
                            else:
                                pPlayer.changeVespeneGas(CivcraftSCResources.getResourcesForHarvest(pPlot, pUnit.getOwner()))

                            pLoopUnit.setHarvestTurns(0)
                        pLoopUnit.finishMoves()

                    return True # We handled the AI, so don't let the SDK try handling it
                # End added by Gerikes
                return False

	def AI_doWar(self,argsList):
		eTeam = argsList[0]
		return False

	def AI_doDiplo(self,argsList):
		ePlayer = argsList[0]
		return False

	def calculateScore(self,argsList):
		ePlayer = argsList[0]
		bFinal = argsList[1]
		bVictory = argsList[2]
		
		iPopulationScore = CvUtil.getScoreComponent(gc.getPlayer(ePlayer).getPopScore(), gc.getGame().getInitPopulation(), gc.getGame().getMaxPopulation(), gc.getDefineINT("SCORE_POPULATION_FACTOR"), True, bFinal, bVictory)
		iLandScore = CvUtil.getScoreComponent(gc.getPlayer(ePlayer).getLandScore(), gc.getGame().getInitLand(), gc.getGame().getMaxLand(), gc.getDefineINT("SCORE_LAND_FACTOR"), True, bFinal, bVictory)
		iTechScore = CvUtil.getScoreComponent(gc.getPlayer(ePlayer).getTechScore(), gc.getGame().getInitTech(), gc.getGame().getMaxTech(), gc.getDefineINT("SCORE_TECH_FACTOR"), True, bFinal, bVictory)
		iWondersScore = CvUtil.getScoreComponent(gc.getPlayer(ePlayer).getWondersScore(), gc.getGame().getInitWonders(), gc.getGame().getMaxWonders(), gc.getDefineINT("SCORE_WONDER_FACTOR"), False, bFinal, bVictory)
		return int(iPopulationScore + iLandScore + iWondersScore + iTechScore)

	def doHolyCity(self):
		return False

	def doHolyCityTech(self,argsList):
		eTeam = argsList[0]
		ePlayer = argsList[1]
		eTech = argsList[2]
		bFirst = argsList[3]
		return False

	def doGold(self,argsList):
		ePlayer = argsList[0]
		return False

	def doResearch(self,argsList):
		ePlayer = argsList[0]
		return False

	def doGoody(self,argsList):
		ePlayer = argsList[0]
		pPlot = argsList[1]
		pUnit = argsList[2]
		return False

	def doGrowth(self,argsList):
		pCity = argsList[0]
		return False

	def doProduction(self,argsList):
		pCity = argsList[0]
		return False

	def doCulture(self,argsList):
		pCity = argsList[0]
		return False

	def doPlotCulture(self,argsList):
		pCity = argsList[0]
		bUpdate = argsList[1]
		return False

	def doReligion(self,argsList):
		pCity = argsList[0]
		return False

	def doGreatPeople(self,argsList):
		pCity = argsList[0]
		return False

	def doMeltdown(self,argsList):
		pCity = argsList[0]
		return False
